<?php
namespace app\index\controller;

use think\Request;

/*
 奖励
*/
class Reward extends Common{

	//首页
	public function index(){
		$pm = [
			'agentid' => AGENT_ID,
		];
		$res = \ServerHelper::post('reward_index',$pm);
		$data = $this->J($res);
		return view('',$data['data']);
	}
	
	//向某个上级申请奖励提现，展示页
	public function apply(){
		$pm = [
			'agentid' => AGENT_ID,
			'toagentid' => Request::instance()->param('toagentid'),
		];
		$res = \ServerHelper::post('reward_intoagent_show',$pm);
		$data = $this->J($res);
		if(empty($data['flag'])){
			$this->error($data['msg']);
		}
		$data['data']['toagentid'] = $pm['toagentid'];
		return view('',$data['data']);
	}
	
	//操作提现
	public function withdraw(){
		$req = Request::instance();
		
		if($req->isPost()){
			$pm = [
				'agentid' => AGENT_ID,
				'toagentid' => Request::instance()->param('toagentid'),//get
				'orderlist' => Request::instance()->param('needinorder'),//get
				'repaccounttype' => Request::instance()->param('accounttype'),//post
				'repaccount' => Request::instance()->param('accountid'),//post
				'innum' => Request::instance()->param('amount'),
				'pwd' => Request::instance()->param('pwd'),
			];
			$res = \ServerHelper::post('reward_inreward_xxpay_do',$pm);
			$data = $this->J($res);
			ajax($data['msg'],$data['flag'] == 1);
			
		}else{
			$pm = [
				'agentid' => AGENT_ID,
				'toagentid' => Request::instance()->param('toagentid'),
				'orderlist' => Request::instance()->param('needinorder'),
			];
			$res = \ServerHelper::post('reward_inreward_xxpay_show',$pm);
			$data = $this->J($res);
			if(empty($data['flag'])){ $this->error($data['msg']); }
			return view('',$data['data']);
		}
	}
	
	//提现记录列表
	public function inlist(){
		$req = Request::instance();
		$pm = [
			'agentid' => AGENT_ID,
			'status' => $req->param('status',-1,'intval'),
			'page' => $req->param('page',1,'intval')
		];
		$res = \ServerHelper::post('reward_inreward',$pm);	
		$data = $this->J($res);
		if($req->isAjax()){
			ajax('SUCCESS',true,$data);
		}else{
			return view('',$data);
		}
	}
	
	//状态按钮：取消申请
	public function cancel(){
		$req = Request::instance();
		$pm = [
			'agentid' => AGENT_ID,
			'canceltype' => $req->param('canceltype'),
			'orderid' => $req->param('orderid'),
			'remark' => $req->param('remark')
		];
		$res = \ServerHelper::post('reward_inreward_cancel',$pm);
		$data = $this->J($res);
		ajax($data['msg'],$data['flag'] == 1);
	}
	
	//被奖励人确认已收到奖励
	public function shoudao(){
		$req = Request::instance();
		$pm = [
			'agentid' => AGENT_ID,
			'orderid' => $req->param('orderid')
		];
		$res = \ServerHelper::post('reward_rewarddef',$pm);
		$data = $this->J($res);
		ajax($data['msg'],$data['flag'] == 1);
	}
	
	//奖励支出展示
	public function payout(){
		$req = Request::instance();
		$pm = [
			'agentid' => AGENT_ID,
			'toagentid' => Request::instance()->param('toagentid'),
		];
		$res = \ServerHelper::post('reward_outtoagent_show',$pm);
		$data = $this->J($res);
		if(empty($data['flag'])){
			$this->error($data['msg']);
		}
		
		$tmp = $req->param('needoutorder','');
		if($tmp != ''){
			//网址中的needoutorder为主
			$data['data']['needoutorder'] = $tmp;
		}
		$data['data']['toagentid'] = $pm['toagentid'];
		return view('',$data['data']);
	}
	
	//奖励支出选择下级的收款账号
	public function payout_show(){
		$req = Request::instance();
		
		$needoutorder = $req->param('needoutorder',''); //销账订单号，主动销账没有，被动销账有
		
		$is_zhudong = substr($needoutorder,0,2) == 'RO' ? false : true; //是否主动销账
		
		if($req->isPost()){
			//这里要区分主动销账(上级直接发放)和被动销账(下级申请、上级发放)
			$pingzheng = $req->param('pingzheng');
			if($pingzheng == '') ajax('请上传打款凭证');
			
			if($is_zhudong){
				//主动销账  对了
				$pm = [
					'agentid' => AGENT_ID,
					'toagentid' => $req->param('toagentid'),//get
					'orderlist' => $needoutorder,//get
					'repaccounttype' => $req->param('accounttype'),//post
					'repaccount' => $req->param('accountid'),//post
					'outnum' => $req->param('amount'),
					'pwd' => $req->param('pwd'),
					'reppicture' => json_encode(explode(',',$pingzheng))
				];
				$res = \ServerHelper::post('reward_outreward_xxpay_do',$pm);
			}else{
				//被动销账 needoutorder RO开头
				$pm = [
					'agentid' => AGENT_ID,
					'orderlist' => $needoutorder,
					'reppicture' => json_encode(explode(',',$pingzheng)),
					'remark' => ''
				];
				$res = \ServerHelper::post('^/reward/xz_do.tsl',$pm);
			}
			
			$data = $this->J($res);
			ajax($data['msg'],$data['flag'] == 1);
			
		}else{
			$pm = [
				'agentid' => AGENT_ID,
				'toagentid' => $req->param('toagentid'),
				'orderlist' => $needoutorder,
				'is_zhudong' => $is_zhudong ? 1 : 0, //是否主动销账
			];
			$res = \ServerHelper::post('reward_outreward_xxpay_show',$pm);
			$data = $this->J($res);
			if(empty($data['flag'])){ $this->error($data['msg']); }
			if(empty($data['data']['repaccount'])){
				$this->error('下级没有设置收款账号');
			}
			return view('',$data['data']);
		}
	}
	
	//支出的记录
	public function outlist(){
		$req = Request::instance();
		$pm = [
			'agentid' => AGENT_ID,
			'status' => $req->param('status',-1,'intval'),
			'page' => $req->param('page',1,'intval')
		];
		$res = \ServerHelper::post('reward_outreward',$pm);
		$data = $this->J($res);
		
		if($req->isAjax()){
			ajax('SUCCESS',true,$data);
		}else{
			return view('',$data);
		}
	}
	
	//查看打款凭证
	public function checkrep(){
		$req = Request::instance();
		$pm = [
			'agentid' => AGENT_ID,
			'orderid' => $req->param('orderid')
		];
		$res = \ServerHelper::post('reward_checkrep',$pm);
		$data = $this->J($res);
		
		return view('',$data['data']);
	}
	
	//奖励详情
	public function detail(){
		$req = Request::instance();
		//apply下级向上级申请奖励时查看的   pay则是上级主动发放奖励时去看的
		$type = $req->param('type','apply'); 
		
		$pm = [
			'agentid' => AGENT_ID,
			'toagentid' => $req->param('toagentid')
		];
		$api = $type == 'apply' ? 'reward_intoagent_list' : 'reward_outtoagent_list';
		$res = \ServerHelper::post($api,$pm);
		$data = $this->J($res);
		
		
		
		if($type == 'apply'){
			return view('detail_apply',$data['data']);
		}else{
			return view('detail_pay',$data['data']);
		}
		
	}
	
	
}
